
define(["js/mCard.js","js/mRule.js","js/mBox.js"],function(card,Rule,box){  
	
	
	var container,context,stage,timer,isBombAnmi=false;
	
	
	function createCanvas(w,h){
		$("#scene").append("<div id='container' style='display:none;width:"+w+"px; height:"+h+"px'></div>");
		var $container = $("#container"),container = Quark.getDOM("container");
		$container.width(w).height(h);
		var canvas = Quark.createDOM("canvas", {width:w, height:h, style:{position:"absolute"}});		
		container.appendChild(canvas);
		context = new Quark.CanvasContext({canvas:canvas});	
		stage = new Quark.Stage({width:w, height:h, context:context});
		timerFun();
	}
	
	function timerFun(){
		timer = new Q.Timer(1000/60);
		timer.addListener(stage);
		timer.addListener(Q.Tween);
		timer.start();
	}
	
	
	
	var Bomb = {
			init:function(obj,num,bombPeople){
				$("#container").remove();
				this._pro();
				isBombAnmi = true;
				createCanvas(this._w,this._h);
				$("#container").show();
				this.timerFun();
				
				this.createLight();
				this.createPeople(bombPeople);
				this.createRocket();
				this.createBomb();
				for(var i=0; i<obj.length; i++){
					this.createBombPeople(obj[i],num);
				}
			},
			
			
			
			addBombImg:function(){
				var img = []
				for(var i=0; i<this._cTyoeArr.length; i++){
					if($("#"+this._cTyoeArr[i]).length===0){
						img.push("<img id='"+this._cTyoeArr[i]+"' src='images/anmi/"+this._cTyoeArr[i]+".png' style='display:none;' />");
					}
				}
				$("body").append(img.join(""));
			},

			

			timerFun:function(){
				timer = new Q.Timer(60);
				timer.addListener(stage);
				timer.addListener(Q.Tween);
				timer.start();
			},

			_pro:function(){
				this._allNum = 500;
				this._allLightNum = 200;
				this._w = parseFloat($("#scene").css("width"));
				this._h = parseFloat($("#scene").css("height"));
				this._cTyoeArr = ["bomb_1","bomb_2","bomb_3","bomb_4","bomb_5","bomb_6","bomb_7","bomb_8"];
			},

			createBomb:function(){
				while(this._allNum--){
					this.dian(this._allNum);
				}
			},
			
			createLight:function(){
				$("#container").addClass("containerBg");
				while(this._allLightNum--){
					this.light(this._allLightNum);
				}
			},

			dian:function(i){
				
				var c =  this._randamNum(0,8);
				var turn = new Q.Bitmap({image:Q.getDOM(this._cTyoeArr[c] )});
				turn.x = this._w/2;
				turn.y = this._h/2;
				var size =  1-Math.random()
				turn.scaleY =size;
				turn.scaleX = size;
				turn.alpha=0;
				stage.addChild(turn);
				setTimeout(function(){
					turn.alpha=1;
					var MaxX = Math.cos(i) * Math.random() * 3400;
					var MaxY = Math.sin(i) * Math.random() * 3400; 
					Q.Tween.to(turn, {x:MaxX,y:MaxY,alpha:0,scaleY:1,scaleX:1,rotation:360}, {time:1000, delay:0,
						onComplete:function(){
							stage.removeChild(turn);
						}});
				},1800)
			},
			
			light:function(){
				
				var light = new Q.Bitmap({image:Q.getDOM("light")});
				var x = this._w - Math.random() * 8000;
				light.x = x;
				light.y =	140+Math.random() * 180;
				var size =  1-Math.random();
				light.scaleY =size;
				light.scaleX = size;
				stage.addChild(light);
				
				Q.Tween.to(light, {x:x+8000}, {time:2000,loop:false,
					onComplete:function(tween){
						$("#container").removeClass("containerBg");
						stage.removeChild(light);
					}});

			},
			
			createPeople:function(bombPeople){
				console.log(bombPeople);
				var _t = this;
				if(bombPeople === "my"){
					var roleName = dataStorage.get(MyOwner.roleName);
				}else if(bombPeople === "right"){
					var roleName = dataStorage.get(RightPlayer.roleName);
				}else if(bombPeople === "left"){
					var roleName = dataStorage.get(LeftPlayer.roleName);
				}
				var id=roleName+"PeopleId";
				var l = parseInt($("#"+id).attr("w"));
				
				var people = new Q.Bitmap({image:Q.getDOM(id)});
				stage.addChild(people);
				people.x = -l;
				people.y = 6;
				console.log("w："+_t._w);
				console.log("l："+l);
				console.log("滑动的位置是："+_t._w-l+50);
				Q.Tween.to(people, {x:(_t._w-l)/2}, {time:600, delay:200, 
					onComplete:function(tween){
						setTimeout(function(){
							stage.removeChild(people);
						},1200);
					}});
			},
			
			
			createRocket:function(){
				var _t = this;
				var rocket = new Q.Bitmap({image:Q.getDOM("rocketBomb")});
				stage.addChild(rocket);
				rocket.x = (this._w-200)/2;
				rocket.y = _t._h;
				Q.Tween.to(rocket, {y:-_t._h}, {time:600,delay:800, 
					onComplete:function(tween){
						stage.removeChild(rocket);
					}});
			},
			
			createBombPeople:function(obj,num){
				if(obj === "my"){
					var roleName = dataStorage.get(MyOwner.roleName);
					var pos = [68,parseFloat($("#scene").css("height"))-318,-1,78];
				}else if(obj === "right"){
					var roleName = dataStorage.get(RightPlayer.roleName);
					var pos = [parseFloat($("#scene").css("width"))-143,-15,1,0];
					
				}else if(obj === "left"){
					var roleName = dataStorage.get(LeftPlayer.roleName);
					var pos = [68,-15,-1,78];
				}
				var id=roleName+"Id";
				if($("#"+id).length === 0){
					var img = "<img id='"+id+"' src='images/"+roleName+"/bombPeople.png' style='display:none;' />";
					$("body").append(img);
				}
				var _t = this;
				setTimeout(function(){
					_t.anmiBombPeople(pos,id,obj,num,roleName);
					isBombAnmi = false;
				},2400)
				
				
			},
			
			anmiBombPeople:function(pos,id,obj,num,roleName){
				people = new Quark.MovieClip({image:Q.getDOM(id), interval:200});
				people.addFrame([
				         	{rect:[0,0,157,203]},
				         	{rect:[157,0,157,203]},
				         	{rect:[0,0,157,203]},
				         	{rect:[157,0,157,203]},
				         	{rect:[0,0,157,203],  stop:1}]);
				people.x = pos[0];
				people.y = pos[1];
				people.scaleX = pos[2];
				people.regX = pos[3];
				stage.addChild(people);
				console.log("人物的的位置是X："+pos[0]);
				
				$("."+obj+" .headIcon").hide();
				Q.Tween.to(people, {alpha:0}, {time:1000,delay:500, 
					onComplete:function(tween){
						stage.removeChild(people);
						$("."+obj+" .headIcon").show();
						//得到比赛类型  用来显示不同比赛类型所表现的头像
						var gameModeType = dataStorage.get(OtherThing.gameModeType);
						//普通场只能显示炸一次的头像
						if(gameModeType === "1"){
							var rolePeopleNum = 2;
						}else{
							var rolePeopleNum = num+1;
						}
						console.log("该角色炸弹的数量："+rolePeopleNum+"---->"+num);
						/*把头像进行本地存储*/
						
						player.setInfo().peopleHead(obj, roleName, rolePeopleNum);
						$("#container").remove();
					}});
			},

			_randamNum:function(min,max){
				return parseInt(Math.random()*max+min)
			}
	}
	
	
	var runCat = {
			init:function(){
				createCanvas(100,60);
				$("#container").show();
				this.timerFun();
				this.runing();
			},
			
			timerFun:function(){
				timer = new Q.Timer(60);
				timer.addListener(stage);
				timer.addListener(Q.Tween);
				timer.start();
			},
			
			runing:function(){
				var people = new Quark.MovieClip({image:Q.getDOM("catImg"), interval:200});
				people.addFrame([
				         	{rect:[0,0,100,60]},
				         	{rect:[100,0,100,60]}]);
				people.x = 0;
				people.y = 0;
				
				stage.addChild(people);
				
			}
	}
	
	
	var result = {
			init:function(r,obj){
				
				var id = $("#result");
				if(id.length !== 0){
					id.remove();
					}
					
				/*显示结算信息*/
				//var doubleCount = obj.doubleCount;
				var stake = obj.stake;
				var winBean = obj.myWinChipsCount; // 当前这盘输赢了多少乐豆
				var currChipsCount = obj.currChipsCount;
				var canGoOn = obj.canGoOn; //是否有足够的乐豆，继续在该房间
				var winScrore = obj.playerWinScrore;
				var myScrore = obj.currPlayerScrore;
				var beanfunLowerLimit = obj.beanfunLowerLimit ;//房间下限
				var beanfunUpperLimit =  obj.beanfunUpperLimit;//房间上限
				console.log(obj);
				//比赛模式
				//游戏模式 1为普通场，2为比赛场
				var gameModeType = obj.gameModeType;
				// 不能继续打牌的原因
				// 0为能继续，1为豆不足，2为比赛已满局，3为比赛时间已到结束了
				var reasonForCanNotGoOn = obj.reasonForCanNotGoOn ; 
				
				//救济金
				var alms = obj.alms;
				var supplementCount = obj.remainedBeanfunSupplementCount;
				
				/*得到倍数*/
				var allBombNums = dataStorage.get(OtherThing.bombNums);
				var multiple = dataStorage.get(MyOwner.multiple);
				var doubleCount = Math.pow(2,allBombNums) * multiple;
				
				if(r === "win"){
					var html = "<div id='result' class='result'>\
						<img class='resultImg resultWin' src='images/xy/img_win.png' />\
						<div class='win resultInfo'>\
								<ul  class='resultTextPokey'>\
									<li class='resultTextPokey-double'><span class='text-bei num_icon'></span>"+card.getNumGif("x"+doubleCount,'blue')+"</li>\
									<li><span class='resultTextPokey-text'>金币：</span>"+card.getNumGif(winBean,'blue')+"</li>\
								</ul>\
							<div class='resultButton'>\
							</div>\
						</div>\
					</div>";
				}else{
					var html = "<div id='result' class='result'>\
						<img class='resultImg resultLose' src='images/xy/img_lose.png' />\
						<div class='lose resultInfo'>\
								<ul  class='resultTextPokey'>\
									<li class='resultTextPokey-double'><span class='text-bei num_icon'></span>"+card.getNumGif("x"+doubleCount,'blue')+"</li>\
									<li><span class='resultTextPokey-text'>金币：</span>"+card.getNumGif(winBean,'blue')+"</li>\
								</ul>\
							<div class='resultButton'>\
							</div>\
						</div>\
					</div>";
				}
				
				$("#scene").append(html);
				
				//box.alms(supplementCount,alms);
				
				var resultName = $("#result .resultMy-name");
				
					//每次结算时去修改乐豆数
					require(["js/mCard.js","js/mPage.js"], function(card,page){ 
						$("#user-dou").html(currChipsCount);
						$("#pokeyMyBox .pokeyDou").html(card.getNumGif(currChipsCount,"blue"));
						card.setRoom(currChipsCount);
						
						/*修改基分*/
						var baseScore = dataStorage.get(OtherThing.baseScore);
						player.setBaseScore(baseScore);
						console.log("Q-->剩余的乐豆："+currChipsCount);
						//发生请求判断有没有新房间打开
					
						$(".toolTrusteeship").addClass("gay");
						$(".toolMenu").removeClass("gay");
						//设置头像
						//player.setInfo().peopleHead("my", roleName, 1);
						
					});
					
					
					/*少于该房间  大于该房间*/
					if(currChipsCount<beanfunLowerLimit){
						$().alert({
							"content" : "您的金币数低于进入该房间的条件，请达到"+beanfunLowerLimit+"金币再来"
						});
						var button = "<input name='' type='button' value='返 回'  class='pokeyButton button-blue' id='reslutReturn'/>" +
						"<input name='' type='button' value='充 值'  class='pokeyButton button-blue' id='reslutRecharge'/>";
					}else if(currChipsCount>=beanfunLowerLimit && currChipsCount<beanfunUpperLimit){
						var button = "<input name='' type='button' value='返 回'  class='pokeyButton button-blue' id='reslutReturn'/>" +
						"<input name='' type='button' value='换 桌'  class='pokeyButton button-blue' id='reslutOther'/>" +
						"<input name='' type='button' value='继 续'  class='pokeyButton button-blue' id='reslutOnce'/>";
					}else{
						$().alert({
							"content" : "您的金币数已经超越该房间的上限，请挑战更高底分的房间"
						});
						var button = "<input name='' type='button' value='返 回'  class='pokeyButton button-blue' id='reslutReturn'/>";
					}
				

								
				var resultData = $("#result .resultButton");
				resultData.append(button);
				
				/*再来一次*/
				$("#reslutOnce").click(function(){
					room.once();
				});
				
				/*继续比赛*/
				$("#goOnGame").click(function(){
					room.goOnGame();
				});
				
				/*换桌*/
				$("#reslutOther").click(function(){
					room.changeDesk();							   
				});
				
				/*充值*/
				$("#reslutRecharge").click(function(){
					//msgBox.chargeMall();							   
				});
				
				/*退出*/
				$("#reslutReturn").click(function(){
					room.goin();
					window.location.reload();
					/*
					require(["js/mPage.js"], function(page){ 
						page.isOpenRoom();
						$("#points").circleScroll({
							"d":-card.getMinRoom(currChipsCount),
							"prev":$(".left-button"),
							"next":$(".right-button")
						});
					});*/
				});
			},
			
			boss:function(r,obj){
				var id = $("#result");
				if(id.length !== 0){
					id.remove();
					}
					
				/*显示结算信息*/
				
				var candy = obj.myWinCandyCount;
				var star = obj.myWinStarCount; // 当前这盘输赢了多少乐豆

				
				
				if(r === "win"){
					var html = "<div id='result' class='result bossResult'>\
						<img class='resultBg' src='images/winBg.png' /><img class='resultImg resultWin' src='images/win.png' />\
						<div class='win resultInfo' >\
								<ul  class='resultTextPokey'>\
									<li><span class='icon_tang icon'></span>"+card.getNumGif(candy,'blue')+"</li>\
									<li><span class='icon_xing icon'></span>"+card.getNumGif(star,'blue')+"</li>\
								</ul>\
							<div class='resultButton'>\
							</div>\
						</div>\
					</div>";
				}else{
					var html = "<div id='result' class='result'>\
						<img class='resultBg' src='images/loseBg.png' /><img class='resultImg resultLose' src='images/lose.png' />\
						<div class='lose resultInfo' >\
								<span class='result_icon loseAnmi loseAnmi_1'></span>\
								<span class='result_icon loseAnmi loseAnmi_2'></span>\
								<span class='result_icon loseAnmi loseAnmi_3'></span>\
								<span class='result_icon loseAnmi loseAnmi_4'></span>\
							<div class='resultButton'>\
							</div>\
						</div>\
					</div>";
				}
				
				$("#scene").append(html);
				
				
					
				if(r === "win"){
					var button = "<span id='reslutRole'   class='pokeyButton pokeyButton-3 button-blue-3 icon' ><span class='text-ghjs num_icon'></span></span>" +
								"<span id='reslutReturn' class='pokeyButton pokeyButton-3 button-blue-3  result-button-comeon icon' ><span class='text-hdt num_icon'></span></span>";
				}else{
					var button = "<span id='reslutReturn'    class='pokeyButton  pokeyButton-3 button-blue-3 icon' /><span class='text-hdt num_icon'></span></span>";
				}
				

								
				var resultData = $("#result .resultButton");
				resultData.append(button);
				
				
				
				/*更换角色*/
				$("#reslutRole").click(function(){
					var forTime=(new Date()).getTime();
					window.location.href = Game.getBaseURL()+"/role.jsp?"+forTime;						   
				});
				
				/*再来一次*/
				$("#reslutAgain").click(function(){
					room.once();						   
				});
				
				/*返回大厅*/
				$("#reslutReturn").click(function(){
					var forTime=(new Date()).getTime();
					window.location.href = Game.getBaseURL()+"/index.jsp?"+forTime;
				});
			},
			
			
			game:function(obj){
				/*
				 * {"competitionResultScore":0,//属于的分
				 * "competitionResult":true,//是否被淘汰
				 * "competitionResultRewardCoupons":0,是否有礼券
				 * "competitionResultRewardBeanfun":5000,//奖励的斗
				 * "type":10000061,
				 * "competitionResultRank":2} //排名*/
				var competitionResult = obj.competitionResult;
				var competitionResultRank = obj.competitionResultRank;
				var competitionResultRewardBeanfun = obj.competitionResultRewardBeanfun;
				
				if(competitionResult){
					var text = "经过激烈战斗，您获得"+competitionResultRank+"排名， 奖励您"+competitionResultRewardBeanfun+"金币，继续巩固您的 地位吧！";
					var html = "<div id='result' class='result'>\
						<span class='resultRank resultRank-win game_reslut_icon'><span class='resultRankNum'>"+card.getNumGif(competitionResultRank,'win')+"</span></span>\
						<img class='resultBg' src='images/winBg.png' /><img class='resultImg resultWin' src='images/win.png' />\
						<div class='win resultInfo'>\
							<div class='resultButton'>\
								<span id='game-home'   class='pokeyButton pokeyButton-3 button-blue-3 icon' ><span class='text-fhdt num_icon'></span></span>\
								<span id='game-sign'   class='pokeyButton pokeyButton-3 button-blue-3 icon' ><span class='text-zcbm num_icon'></span></span></div>\
						</div>\
					</div>";
				}else{
					var text ="经过激烈战斗，您遗憾的败下来，  只得到了战利品：金币"+competitionResultRewardBeanfun+",快报名 下场复仇吧！";
					var html = "<div id='result' class='result'>\
						<span class='resultRank resultRank-lose game_reslut_icon'><span class='resultRankNum'>"+card.getNumGif(competitionResultRank,'lose')+"</span></span>\
						<img class='resultBg' src='images/loseBg.png' /><img class='resultImg resultLose' src='images/lose.png' />\
						<div class='lose resultInfo'>\
							<div class='resultButton'>\
								<span id='game-home'   class='pokeyButton pokeyButton-3 button-blue-3 icon' ><span class='text-fhdt num_icon'></span></span>\
								<span id='game-sign'   class='pokeyButton pokeyButton-3 button-blue-3 icon' ><span class='text-zcbm num_icon'></span></span></div>\
						</div>\
					</div>";
				}
				$("#scene").append(html);
				
				setTimeout(function(){
					box.gameReslute(text);
				},1000);
				
				$("#game-home").live("click",function(){
					pokeyEffect.closeResult();
					$("#room").show();
					$("#playBox").hide();
					require(["js/mGame.js"], function(game){
						game.gameStat(1)
					});
					
				});
				
				$("#game-sign").live("click",function(){
					pokeyEffect.closeResult();
					$("#room").show();
					$("#playBox").hide();
					require(["js/mGame.js"], function(game){
						game.sign();
					});
				});
			}
	}
	
	
	
	
	
	return {
		addBombNums:function(obj){
			if(obj==="my"){
				var myBombNums = parseInt(dataStorage.get(MyOwner.bombNums))+1;
				dataStorage.set(MyOwner.bombNums,myBombNums);
				//如果是地主  
				if(dataStorage.get(MyOwner.isLandlord)==="true"){
					Bomb.init(["left","right"],myBombNums,obj);
					//bomb("right",myBombNums);
				}else{
					if(dataStorage.get(LeftPlayer.isLandlord)==="true"){
						Bomb.init(["left"],myBombNums+parseInt(dataStorage.get(RightPlayer.bombNums)),obj);
					}else{
						Bomb.init(["right"],myBombNums+parseInt(dataStorage.get(LeftPlayer.bombNums)),obj);
					}
				}
			}else if(obj ==="right"){
				var rightBombNums = parseInt(dataStorage.get(RightPlayer.bombNums))+1;
				dataStorage.set(RightPlayer.bombNums,rightBombNums);
				
				if(dataStorage.get(RightPlayer.isLandlord)==="true"){
					Bomb.init(["left","my"],rightBombNums,obj);
					//bomb(["my"],rightBombNums);
				}else{
					if(dataStorage.get(MyOwner.isLandlord)==="true"){
						Bomb.init(["my"],parseInt(dataStorage.get(LeftPlayer.bombNums))+rightBombNums,obj);
					}else{
						Bomb.init(["left"],parseInt(dataStorage.get(MyOwner.bombNums))+rightBombNums,obj);
					}
				}
			}else if(obj === "left"){
				var leftBombNums = parseInt(dataStorage.get(LeftPlayer.bombNums))+1;
				dataStorage.set(LeftPlayer.bombNums,leftBombNums);
				
				if(dataStorage.get(LeftPlayer.isLandlord)==="true"){
					Bomb.init(["right","my"],leftBombNums,obj);
					//bomb("my",leftBombNums);
				}else{
					if(dataStorage.get(MyOwner.isLandlord)==="true"){
						Bomb.init(["my"],leftBombNums+parseInt(dataStorage.get(RightPlayer.bombNums)),obj);
					}else{
						Bomb.init(["right"],parseInt(dataStorage.get(MyOwner.bombNums))+leftBombNums,obj);
					}
				}
			}
			
			/*设置倍数*/
			var allBombNums = parseInt(dataStorage.get(MyOwner.bombNums))+parseInt(dataStorage.get(RightPlayer.bombNums))+parseInt(dataStorage.get(LeftPlayer.bombNums));
			dataStorage.set(OtherThing.bombNums,allBombNums);
			/*得到倍数*/
			var multiple = dataStorage.get(MyOwner.multiple);
			var all = Math.pow(2,allBombNums) * multiple;
			console.log("得到倍数:"+all);
			$("#pokeyMyBox .pokeyMultiple").show().html(card.getNumGif("x"+all,"blue"));
		},
		
		
		result:function(r, obj){
			if(isBombAnmi){
				console.log("最后一把炸弹");
				setTimeout(function(){
					result.init(r, obj);
				},9000);
			}else{
				console.log("最后没有炸弹");
				setTimeout(function(){
					result.init(r, obj);
				},3500);
			}
		},
		
		bossResult:function(r, obj){
			if(isBombAnmi){
				console.log("最后一把炸弹");
				setTimeout(function(){
					result.boss(r, obj);
				},9000);
			}else{
				console.log("最后没有炸弹");
				setTimeout(function(){
					result.boss(r, obj);
				},3500);
			}
		},
		
		//比赛房每一盘的结算
		gameResult:function(r, obj){
			if(isBombAnmi){
				console.log("最后一把炸弹");
				setTimeout(function(){
					require(["js/mGame.js"], function(game){  
						game.gameStat(3);
					});
				},9000);
			}else{
				console.log("最后没有炸弹");
				setTimeout(function(){
					require(["js/mGame.js"], function(game){  
						game.gameStat(3);
					});
				},3000);
			}
		},
		
		//比赛房胜利失败的结算
		gameLastResult:function(obj){
			
			result.game(obj);	
			
		},
		
		runCat:function(){
			runCat.init();
		},
		
		/*开启BOSS房间*/
		openBoss:function(){
			var room = $("#room_boss");
			room.addClass("jumpAnmi");
		},
		
		/*开启比赛场房间*/
		openCompetition:function(){
			var room = $("#room_bs");
			room.addClass("jumpAnmi");
		},
		
		/*开启乐斗房间*/
		openBeanfun:function(){
			var room = $("#room_ld");
			room.addClass("jumpAnmi");
		}
		
	}
}); 